package com.java110.mall.cart.cmd.storeOrder;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.app.AppDto;
import com.java110.dto.housekeeping.HousekeepingServPoolDto;
import com.java110.dto.storeOrder.StoreOrderCartFeeDto;
import com.java110.intf.cart.IHousekeepingServPoolInnerServiceSMO;
import com.java110.intf.cart.IStoreOrderCartFeeInnerServiceSMO;
import com.java110.intf.dev.IAppV1InnerServiceSMO;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.ArrayList;
import java.util.List;

@Java110Cmd(serviceCode = "storeOrder.queryAdminStoreOrderCartFee")
public class QueryAdminStoreOrderCartFeeCmd extends Cmd {

    @Autowired
    private IStoreOrderCartFeeInnerServiceSMO storeOrderCartFeeInnerServiceSMOImpl;

    @Autowired
    private IHousekeepingServPoolInnerServiceSMO housekeepingServPoolInnerServiceSMOImpl;
    @Autowired
    private IAppV1InnerServiceSMO appInnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {

        super.validatePageInfo(reqJson);
        super.validateAdmin(context);
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        StoreOrderCartFeeDto storeOrderCartFeeDto = BeanConvertUtil.covertBean(reqJson, StoreOrderCartFeeDto.class);

        int count = storeOrderCartFeeInnerServiceSMOImpl.queryStoreOrderCartFeesCount(storeOrderCartFeeDto);

        List<StoreOrderCartFeeDto> storeOrderCartFeeDtos = null;
        if (count > 0) {
            storeOrderCartFeeDtos = storeOrderCartFeeInnerServiceSMOImpl.queryStoreOrderCartFees(storeOrderCartFeeDto);
            freshAppName(storeOrderCartFeeDtos);
        } else {
            storeOrderCartFeeDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) storeOrderCartFeeDto.getRow()), count, storeOrderCartFeeDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        context.setResponseEntity(responseEntity);

    }

    private void freshAppName(List<StoreOrderCartFeeDto> storeOrderCartFeeDtos) {

        if (storeOrderCartFeeDtos == null || storeOrderCartFeeDtos.size() < 1) {
            return;
        }

        List<String> appIds = new ArrayList<>();
        for (StoreOrderCartFeeDto storeOrderCartFeeDto : storeOrderCartFeeDtos) {
            appIds.add(storeOrderCartFeeDto.getPayWay());
        }

        AppDto appDto = new AppDto();
        appDto.setAppIds(appIds.toArray(new String[appIds.size()]));
        List<AppDto> appDtos = appInnerServiceSMOImpl.queryApps(appDto);

        for (StoreOrderCartFeeDto storeOrderCartFeeDto : storeOrderCartFeeDtos) {
            for (AppDto tmpAppDto : appDtos) {
                if (tmpAppDto.getAppId().equals(storeOrderCartFeeDto.getPayWay())) {
                    storeOrderCartFeeDto.setPayWayName(tmpAppDto.getName());
                }
            }
        }
    }

    private void refreshCartId(StoreOrderCartFeeDto storeOrderCartFeeDto) {
        HousekeepingServPoolDto housekeepingServPoolDto = new HousekeepingServPoolDto();
        housekeepingServPoolDto.setPoolId(storeOrderCartFeeDto.getPoolId());
        housekeepingServPoolDto.setShopId(storeOrderCartFeeDto.getShopId());
        List<HousekeepingServPoolDto> housekeepingServPoolDtos = housekeepingServPoolInnerServiceSMOImpl.queryHousekeepingServPools(housekeepingServPoolDto);
        if (housekeepingServPoolDtos == null || housekeepingServPoolDtos.size() < 1) {
            return;
        }
        storeOrderCartFeeDto.setCartId(housekeepingServPoolDtos.get(0).getCartId());
    }

}
